﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Itfort.Web
{
	/// <summary>
	/// 
	/// </summary>
	[DefaultProperty("Text")]
	[ToolboxData("<{0}:WebPager runat=\"server\"></{0}:WebPager>")]
	public class WebPager : WebControl
	{
		/// <summary>
		/// 
		/// </summary>
		public WebPager()
		{
			PageIndex = 1;
			PageSize = 10;
			NumericSize = 5;
			UrlPattern = "#{0}";
		}

		/// <summary>
		/// 
		/// </summary>
		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string Text
		{
			get
			{
				Int32 _PageCount = this.PageCount;
				if (_PageCount < 2)
				{
					return "";
				}

				int startCount = 1;
				int endCount = NumericSize;
				if (_PageCount <= NumericSize)
				{
					endCount = _PageCount;
				}
				else if (PageIndex * 2 > NumericSize)
				{
					startCount = PageIndex - (NumericSize / 2) + 1;
					endCount = startCount + NumericSize - 1;

					if (endCount > _PageCount)
					{
						int tempI = endCount - _PageCount;
						endCount = _PageCount;
						startCount = startCount - tempI;
					}
				}

				StringBuilder text = new StringBuilder();
				text.Append(string.Format("<ul>\n"));

				// 处理上页
				if (PageIndex > 1)
				{
					text.Append(string.Concat("<li><a href=\"", String.Format(UrlPattern, PageIndex - 1), "\">上一页</a></li>\n"));
				}
				else
				{
					text.Append(string.Concat("<li class=\"nolink\">上一页</li>\n"));
				}

				if (startCount > 1)
				{
					text.Append(String.Concat("<li><a href=\"", String.Format(UrlPattern, 1), "\" >" + WrapNumber(1) + "</a></li>\n"));

					if (startCount - 1 > 1)
					{
						text.Append(String.Concat("<li><span>...</span></li>\n"));
					}
				}

				for (int i = startCount; i <= endCount; i++)
				{
					if (i == PageIndex)
					{
						text.Append(String.Concat("<li class=\"current\">", WrapNumber(i), "</li>\n"));
						continue;
					}

					text.Append(String.Concat("<li><a href=\"", String.Format(UrlPattern, i), "\" >", WrapNumber(i), "</a></li>\n"));
				}

				if (endCount < _PageCount)
				{
					if (_PageCount - endCount > 1)
					{
						text.Append(String.Concat("<li><span>...</span></li>\n"));
					}

					text.Append(String.Concat("<li><a href=\"", String.Format(UrlPattern, _PageCount), "\" >", WrapNumber(this.PageCount), "</a></li>\n"));
				}

				// 处理下页
				if (PageIndex + 1 <= _PageCount)
				{
					text.Append(String.Concat("<li><a href=", String.Format(UrlPattern, PageIndex + 1), ">下一页</a></li>\n"));
				}
				else
				{
					text.Append("<li class=\"nolink\">下一页</li>\n");
				}

				if (true)
				{
					text.Append(string.Format("<li class=\"sum\">共&nbsp;<strong>{0}</strong>&nbsp;条记录 当前&nbsp;<strong>{1}</strong>&nbsp;/&nbsp;{2}</li>\n ", TotalRowCount, PageIndex, _PageCount));
				}

				text.Append("</ul>");
				return text.ToString();
			}

			set
			{
				ViewState["Text"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="output"></param>
		protected override void RenderContents(HtmlTextWriter output)
		{
			output.Write(Text);
		}

		/// <summary>
		/// 
		/// </summary>
		public void LoadParams()
		{
			var Request = HttpContext.Current.Request;
			Int32 xPageIndex = Request.ToInt32("page");
			Int32 xPageSize = Request.ToInt32("size");
			if (xPageIndex > 0) this.PageIndex = xPageIndex;
			if (xPageSize > 0) this.PageSize = xPageSize;

			String queryString = "page={0}";
			foreach (var key in Request.QueryString.AllKeys)
			{
				if (key.ToLower() == "page") continue;
				String kv = key + "=" + Request.QueryString[key];
				queryString = queryString + "&" + kv;
			}
			this.UrlPattern = Request.Path + "?" + queryString;
		}

		/// <summary>
		/// 获取或设置为当前页面的索引值
		/// </summary>
		public Int32 PageIndex { get; set; }

		/// <summary>
		/// 获取或设置为每个数据页显示的记录数
		/// </summary>
		[Browsable(true)]
		public Int32 PageSize { get; set; }

		/// <summary>
		/// 获取或设置页码选择页数量
		/// </summary>
		[Browsable(true)]
		public Int32 NumericSize { get; set; }

		/// <summary>
		/// 获取或设置Url模式。
		/// 如：http://www.itofrt.com/index.aspx?page={0}
		/// </summary>
		[Browsable(true)]
		public String UrlPattern { get; set; }

		/// <summary>
		/// 获取或设置总记录数
		/// </summary>
		public Int32 TotalRowCount { get; set; }

		/// <summary>
		/// 获取页面个数
		/// </summary>
		public Int32 PageCount
		{
			get
			{
				if (TotalRowCount % PageSize == 0)
				{
					return TotalRowCount / PageSize;
				}
				else
				{
					return TotalRowCount / PageSize + 1;
				}
			}
		}

		private String WrapNumber(Int32 i)
		{
			return String.Format("[{0}]", i);
		}
	}
}
